<?php
namespace Home\Controller;
use Lib\ChinesePinyin;
class SystemController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
    $this->Is_power("system");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',cookie('heardadminData'));
		$this->assign('heardadminPower',cookie('heardadminPower'));
		$this->assign('founder',cookie('founder'));
	}
	public function systemsetup(){
		$db_systemsetup = M("systemsetup");
		$setdata = $db_systemsetup->where("type=0")->select();
		// p($setdata);exit();
		$this->assign('setdata',$setdata);
		$this->display();
	}
	public function setting(){
		// p($_POST);exit();
		foreach ($_POST as $key => $value) {
			M("systemsetup")->where(array('id'=>$key))->save(array('value'=>$value));//更新数据
		}
		$this->adminlog("修改系统设置");
		$this->success('修改成功',"/Home/System/systemsetup",1);
	}
	public function termssetup(){
		$db_termssetup = M("termssetup");
		$termsdata = $db_termssetup->select();
		// p($termsdata);exit();
		$this->assign('termsdata',$termsdata);
		$this->display();
	}
	public function termssetting(){
		// p($_POST);exit();
		M("termssetup")->where(array('id'=>1))->save(array('terms'=>$_POST['terms1']));//更新数据
		M("termssetup")->where(array('id'=>2))->save(array('terms'=>$_POST['terms2']));//更新数据
		$this->adminlog("修改协议");
		$this->success('修改成功',"/Home/System/termssetup",1);
	}
	public function levelsetup(){
		$db_systemsetup = M("systemsetup");
		$setdata = $db_systemsetup->where("type=1")->select();
		// p($setdata);exit();
		$this->assign('lvsetdata',$setdata);
		$this->display();
	}
	public function levelsetting(){
		// p($_POST);exit();
		foreach ($_POST as $key => $value) {
			M("systemsetup")->where(array('id'=>$key))->save(array('message'=>$value['0'],'value'=>$value['1'],'money'=>$value['2']));//更新数据
		}
		$this->adminlog("修改等级");
		$this->success('修改成功',"/Home/System/levelsetup",1);
	}
	public function level_add(){
		$this->display();
	}
	//修改等级
	public function add_level()
	{
		$message = I('post.message');
		$value = I('post.value');
		$money = I('post.money');
		if (empty($message)||empty($value)) {
			$this->error('参数错误',"",1);
		}
		$data['message'] = $message;
		$data['value'] = $value;
		$data['money'] = $money;
		$data['type'] = 1;
		// p($data);exit();
		M("systemsetup")->add($data);
		$this->adminlog("添加等级");
		$this->success('创建成功',"/Home/System/level_add",1);
	}
	//删除等级
	public function level_delete()
	{
		$id = I('get.id');
		// echo $id;exit();
		M("systemsetup")->delete($id);
		$this->adminlog("删除等级");
		$this->success('删除成功',"/Home/System/levelsetup",1);
	}

// 驳回列表
	public function failtxtsetup(){
		$db_systemsetup = M("dismiss");
		$setdata = $db_systemsetup->select();
		// p($setdata);exit();
		$this->assign('lvsetdata',$setdata);
		$this->display();
	}
	public function failtxtsetting(){
		// p($_POST);exit();
		foreach ($_POST as $key => $value) {
			M("dismiss")->where(array('id'=>$key))->save(array('message'=>$value['0']));//更新数据
		}
		$this->success('修改成功',"/Home/System/failtxtsetup",1);
	}
	public function failtxt_add(){
		$this->display();
	}
	//修改等级
	public function add_failtxt()
	{
		$message = I('post.message');
		if (empty($message)) {
			$this->error('参数错误',"",1);
		}
		$data['message'] = $message;
		// $data['type'] = 2;
		// p($data);exit();
		M("dismiss")->add($data);
		$this->adminlog("创建驳回文案");
		$this->success('创建成功',"/Home/System/failtxt_add",1);
	}
	//删除等级
	public function failtxt_delete()
	{
		$id = I('get.id');
		// echo $id;exit();
		M("systemsetup")->delete($id);
		$this->adminlog("删除驳回文案");
		$this->success('删除成功',"/Home/System/failtxtsetup",1);
	}

	public function pinyin(){
		if ($_POST) {
			$words = I('post.words');
			$url = "api.heardtech.com/Post/PostWords";
			$arr['words'] = $words;
			$result = do_curl($url,$arr);
			$this->assign('words',$words);
			$this->assign('result',json_decode($result)->data);
		}
		$this->display();
	}
	public function mypinyin(){
		if ($_POST) {
			$words = I('post.words');
			$pinyin = new ChinesePinyin();
			$result = $pinyin->TransformWithoutTone($words,' ');
			$this->assign('words',$words);
			$this->assign('result',$result);
		}
		$this->display();
	}

	public function fanyi(){
		$url = "http://tts.youdao.com/fanyivoice?keyfrom=fanyi%2Eweb%2Eindex&le=eng&word=test";
		$url = "http://fanyi.baidu.com/gettts?lan=en&spd=1&source=web&text=The%20trainees";
		$article = M('article')->where("id=1")->find();
		// $article['article'] = explode(" ", $article['article']);
		// p(strip_tags(html_entity_decode($article['article'])));exit();
		$articledata = strip_tags(html_entity_decode($article['article']));
		$articledata = str_replace("&nbsp;","",$articledata);
		$parrten = "/[a-zA-Z]{3,20}+/";
		preg_match_all($parrten,$articledata,$arr);
		// p($article);exit();
		$arr['0'] = array_unique($arr['0']);
		foreach ($arr['0'] as $key => $value) {
			$str = $value."&lt;a class='play'  onclick=playaudio('".$value."')&gt;&lt;img src='/Public/admin/sncss/images/voice.png'&gt;&lt;/a&gt;";
			$article['article'] = str_replace($value,$str,$article['article']);
		}
		$article['article'] = html_entity_decode($article['article']);
		// p($arr);exit();
		// p($article);exit();
		$this->assign('articledata',$article);
		$this->display();
	}

	//查询分类音频列表
	public function GetCatelist(){
			$data[0]['id'] = 0;
			$data[0]['labelname'] = "热门";
			$data[0]['mediadata'] = M('media')->field('id,media_name,media_pic,token_key,subscript')->where(array('ishot'=>1))->select();
			$url = "http://".ADMIN_URL."Home/Login/Apiarticles";
			$res = do_curl($url);
			// p($res);
			$labelkey = (array)json_decode($res)->data;
			// p($labelkey);
			foreach ($labelkey as $key => $value) {
				$a[] = "'".$key."'";
				// $a[] = substr(substr($key,1),0,-1);
			}
			$label = implode(",", $a);

			$sql = "select id,GROUP_CONCAT(articles) from db_media where articles in ($label)";
			$res = M()->query($sql);
			echo M()->_sql();
			p($res);exit();
			foreach ($labelkey as $key => &$value) {
				$k = substr(substr($key,1),0,-1);
				$data[$k]['id'] = $k;
				$data[$k]['labelname'] = $value;
				$data[$k]['mediadata'] = M('media')->field('id,media_name,media_pic,token_key,subscript')->where(array('articles'=>$key,'ishot'=>0))->select();
				if (count($data[$k]['mediadata'])==0) {
					unset($data[$k]);
				}
			}
			foreach ($data as $key => &$value) {
				$value['num'] = count($value['mediadata']);
				$newdata[] = $value;
			}
			// p($newdata);exit();
			$this->ApiSuccess('ok',$newdata);

	}

// 驳回列表
	public function getsendorder(){
		$db_getsendorder = M("getsendorder");
		$orderdata = $db_getsendorder->order("id desc")->select();
		// p($orderdata);exit();
		$this->assign('orderdata',$orderdata);
		$this->display();
	}
}